#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 10 14:29:13 2019

@author: xubing
"""


import xlsxwriter
#from Scorecard.vars_bining import *
import pandas as pd
#格式化输出

def format_output(df):
    df['all_distrbution'] = df['count'].apply(lambda x: str(int(x))) + '('  + df['all_pct'].apply(lambda x: str('%.4f'%x)) + ')'
    df['p_distribution'] = df['p_count'].apply(lambda x: str(int(x))) + '('  + df['p_pct'].apply(lambda x: str('%.4f'%x)) + ')'
    df['n_distribution'] = df['n_count'].apply(lambda x: str(int(x))) + '('  + df['n_pct'].apply(lambda x: str('%.4f'%x)) + ')'

    return df[['all_distrbution','p_distribution','n_distribution','woe','iv']]

def result2excel(exceltable_name,headings,
                 cols_name,cols_cn_desc,
                 cols_dist1,iv_sum1,
                 cols_dist2,iv_sum2,
                 cols_dist3,iv_sum3):
    '''
    :param exceltable_name:表名
    :param headings: 表头 第一行
    :param A2_col: 第一列
    :param B2_col: 第二列
    :param C2_col: 第三列
    :return:
    '''
    print('===开始写入excel表===')
    workbook = xlsxwriter.Workbook(exceltable_name)  # 新建excel表
    cell_format = workbook.add_format({'bold': True,})#'bg_color':'silver'
    worksheet = workbook.add_worksheet('DATA_DISTRIBUTION')  # 新建sheet（sheet的名称为"sheet1"）
    worksheet.write_row('A1', headings)
    worksheet.write_column('A2', cols_name)
    worksheet.write_column('B2', cols_cn_desc) 
    worksheet.write_column('C2', cols_dist1,cell_format)
    worksheet.write_column('D2', iv_sum1)
    worksheet.write_column('E2', cols_dist2,cell_format)
    worksheet.write_column('F2', iv_sum2)
    worksheet.write_column('G2', cols_dist3,cell_format)
    worksheet.write_column('H2', iv_sum3)
    workbook.close()#没有这行代码会不产生表文件
    print('写入文件成功！表名为：',exceltable_name)
    
def add_cn_desc(df):
    '''增加中文描述'''
    map_dict = {}
    cn_desc_df = pd.read_excel('cn_desc.xlsx')
    for i in range(len(cn_desc_df)):
        map_dict[cn_desc_df.iloc[i,0]] = cn_desc_df.iloc[i,1]
    
#    df['中文描述'] = df['列名'].replace(map_dict)
    df.insert(1,'中文描述',df['列名'].replace(map_dict))
    return df

